/*
 * 
 * Created on Jul 25, 2005
 */
package remato.client.view.editor.usertypes;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ListDialog;
import org.eclipse.ui.dialogs.SelectionDialog;

import remato.client.controller.RequirementPlugin;
import remato.client.model.AbstractPropertySpesification;
import remato.client.model.PropertyDefWrapper;
import remato.client.model.PropertyType;
import remato.client.view.facade.IModelTableEditor;
import remato.domain.common.Category;
import remato.domain.common.PropertyDef;
import remato.domain.common.Requirement;
import remato.common.domain.property.PropertyTypeEnumImpl;

/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: CreateUserTypesTableRowAction.java,v 1.5 2006-03-07 14:56:23 petterei Exp $
 */
class CreateUserTypesTableRowAction extends UserTypesTableAction implements SelectionListener{
	private ListDialog dialog = null;

    public CreateUserTypesTableRowAction(StructuredViewer viewer) {
        super("Create " + PropertyDef.class.getSimpleName());
        init(viewer) ;
    }

    private void init(StructuredViewer viewer) {
        setViewer(viewer);
        setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD)); //TODO: change image
        setToolTipText("Create new "+ AbstractPropertySpesification.class.getSimpleName() +" for " + Requirement.class.getSimpleName() + "s of this " + Category.class.getSimpleName());
        createDialog();
    }

    @Override
    public void run() {
    	if (dialog.open() == SelectionDialog.OK && dialog.getResult().length > 0){
    		PropertyType type = (PropertyType)dialog.getResult()[0];
    		PropertyDef modelDef = RequirementPlugin.getInstance().getSession().getDomainSession().<PropertyDef>createConcept(PropertyDef.class, PropertyTypeEnumImpl.valueOf(type.name()));
    		PropertyDefWrapper spec = RequirementPlugin.getInstance().getSession().getWrapperFactory().getWrapper(modelDef);
        	spec.setDescription("No description set.");
        	spec.setLabel("No label set.");
            ((IModelTableEditor<AbstractPropertySpesification>)getViewer().getContentProvider()).addElement(spec);
            getViewer().setSelection(new StructuredSelection(spec));
            super.run();
    	}
    }

	/**
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetDefaultSelected(SelectionEvent e) {
		RequirementPlugin.log(getClass(), "widgetDefaultSelected(..): SelectionEvent: " + e.getClass());
	}

	/**
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetSelected(@SuppressWarnings("unused") SelectionEvent e) {
		run();
	}

	private void createDialog() {
		dialog = new ListDialog(getViewer().getControl().getShell());
		dialog.setContentProvider(new PropertyTypeSelectionContentProvider());
		dialog.setLabelProvider(new PropertyTypeSelectionLabelProvider());
		dialog.setTitle("Select type for the new PropertyDefinition");
		// TODO: add all...
		dialog.setInput(PropertyType.getPropertyTypes());
	}
}
class PropertyTypeSelectionContentProvider implements IStructuredContentProvider {
	public void inputChanged(@SuppressWarnings("unused") Viewer viewer, @SuppressWarnings("unused") Object oldInput, @SuppressWarnings("unused") Object newInput) {}
	public Object[] getElements(Object inputElement) {
		if (inputElement instanceof PropertyType[]) {
			return (PropertyType[])inputElement;
		}
		return PropertyType.values();
	}
	public void dispose() {}
}
class PropertyTypeSelectionLabelProvider implements ILabelProvider {
	public Image getImage(@SuppressWarnings("unused") Object element) { return null; }
	public String getText(Object element) {
		if (element instanceof PropertyType) {
			PropertyType type = (PropertyType) element;
			return type.getFullDescription();
		}
		return "";
	}
	public void addListener(@SuppressWarnings("unused") ILabelProviderListener listener) {}
	public void dispose() {}
	public boolean isLabelProperty(@SuppressWarnings("unused") Object element, @SuppressWarnings("unused") String property) { return false; }
	public void removeListener(@SuppressWarnings("unused") ILabelProviderListener listener) {}
}
